package com.config;

import com.controller.TestServlet;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.util.Set;

/**
 * @author
 * @date 2025-07-29
 * @Description 替代web.xml 文件 实现0配置
 * 通过 ServletContainerInitializer
 * 告诉tomcat容器，初始化类的位置。META-INF/services
 */
// web 初始化器
public class WebInitializer implements ServletContainerInitializer {

    //当服务器启动时会自动执行当前这个方法。
    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        System.out.println("服务器启动了");
        ServletRegistration.Dynamic test2 = servletContext.addServlet("test2", new TestServlet());
        test2.setLoadOnStartup(0);
        test2.addMapping("/test2");
    }
}
